﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cinema_Ticketing_System.Models;
using Cinema_Ticketing_System.Models.Admin;
using Cinema_Ticketing_System.Models.ViewModel;
using Newtonsoft.Json;

namespace Cinema_Ticketing_System.Controllers
{



    public class AdminController : Controller
    {
        Models.Film.Films films = new Models.Film.Films();
        Get_All_Film get_All_Film = new Get_All_Film();
        Film_Type film_Type = new Film_Type();
        Del_Films del_Films = new Del_Films();
        // GET: Admin
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();

        }

        [HttpGet]
        public ActionResult AddFilm()
        {
            var listtype = film_Type.GetArea();
            ViewBag.listtype = listtype;
            return View();
        }
        //添加信息
        [HttpPost]
        public ActionResult AddFilm(film_table film_Table)
        {
            //如果有一项没有通过验证 则返回
            if (!ModelState.IsValid)
            {
                return Content(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = false, message = "验证失败" }), "application/json");
            }

            ////创建视图类转换未实体类
            //user_table user = new user_table()
            //{
            //    username = film_Table.username,
            //    userpassword = film_Table.newpwd,
            //    userphone = film_Table.userphone,
            //};

            Admin a1 = new Admin();

            //调用用户中的修改用户信息方法
            int rows = a1.AddFilm(film_Table);
            //调用获得用户订单信息的方法

            string message = 1 > 0 ? "添加成功" : "添加失败";
            return Content(message, "text / plain");
        }
        [HttpGet]
        public ActionResult Admin_Del_film(int pageIndex = 1, int requestStatus = 0)
        {
            var listfilm = get_All_Film.GetAllFilm();
            int pagenum = (int)Math.Ceiling(Convert.ToDouble(listfilm.Count) / 6);
            var listPage = listfilm.Skip((pageIndex - 1) * 6).Take(6);

            if (requestStatus == 0)
            {
                ViewBag.listfilm = listfilm;
                ViewBag.listname = listPage;
                ViewBag.pageNum = pagenum;
                return View();
            }
            else
            {
                var list = from s in listPage
                           select new { s.filmid, s.filmname, s.star, s.brief_introduction, s.release_time, s.filmlanguage, s.cover };


                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(list, settings);

                return Content(jsongStr, "application/json");

            }


        }

        [HttpPost]
        public ActionResult Admin_Del_film(string fid)
        {
            film_table film_Table1 = films.GetFilmMsg(Convert.ToInt32(fid));

            int rows = films.DeleteFilm(film_Table1); 

            string msg = rows > 0 ? "删除成功" : "删除失败";
            return Content(msg, "text/plain");

        }
    }
}